package com.sctu.edu.tracks.utils.httputils;

import android.app.Activity;

import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *获取网络数据
 * 
 */
public class GetVolleyHttp {

	private static GetVolleyHttp getjsonvollery = null;
	private Activity activity;
	private RequestQueue request;

	/**
	 * 构造函数
	 * 
	 * @param activity
	 */
	private GetVolleyHttp(Activity activity) {
		this.activity = activity;
		request = Volley.newRequestQueue(activity);
	}

	/**
	 * 获取实例 单列模式
	 * 
	 * @param activity
	 *            当前activity
	 * @return
	 */
	public static GetVolleyHttp getInstance(Activity activity) {
		if (getjsonvollery == null) {
			getjsonvollery = new GetVolleyHttp(activity);
		}
		return getjsonvollery;
	}

	/**
	 * 获取StringJson数据 Get方式
	 * 
	 * @param url
	 *            url地址
	 * @param listener
	 *            成功接口
	 * @param errorlistener
	 *            失败接口
	 */
	public void getStringGETJsonData(final String url,
                                     Listener<String> listener,
                                     ErrorListener errorlistener,
                                     final String token) {
        if(request.getCache().get(url)!=null){
            Long startTime = request.getCache().get(url).serverDate;
            Long nowTime = new Date().getTime();
            if ((nowTime - startTime)> 4*60*60*1000){                                 //每4小时更新一次数据
                StringRequest stringrequest = new StringRequest(Method.GET, url, listener, errorlistener){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<String, String>();
                        if (token != null && !token.equals("")){
                            headers.put("Authorization",token);
                        }
                        return headers;
                    }
                };
                stringrequest.setShouldCache(Boolean.TRUE);
                request.add(stringrequest);
            }else {
                listener.onResponse(new String(request.getCache().get(url).data));
            }
        }else {
            StringRequest stringrequest = new StringRequest(Method.GET, url, listener, errorlistener){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    if (token != null && !token.equals("")){
                        headers.put("Authorization",token);
                    }
                    return headers;
                }
            };
            stringrequest.setShouldCache(Boolean.TRUE);
            request.add(stringrequest);
        }



	}



    public void getStringDELETEJsonData(final String url, Listener<String> listener, ErrorListener errorlistener, final String token) {
        if(request.getCache().get(url)!=null){
            Long startTime = request.getCache().get(url).serverDate;
            Long nowTime = new Date().getTime();
            if ((nowTime - startTime)> 4*60*60*1000){                                 //每4小时更新一次数据
                StringRequest stringrequest = new StringRequest(Method.DELETE, url, listener, errorlistener){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<String, String>();
                        if (token != null && !token.equals("")){
                            headers.put("Authorization",token);
                        }
                        return headers;
                    }
                };
                stringrequest.setShouldCache(Boolean.TRUE);
                request.add(stringrequest);
            }else {
                listener.onResponse(new String(request.getCache().get(url).data));
            }
        }else {
            StringRequest stringrequest = new StringRequest(Method.DELETE, url, listener, errorlistener){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    if (token != null && !token.equals("")){
                        headers.put("Authorization",token);
                    }
                    return headers;
                }
            };
            stringrequest.setShouldCache(Boolean.TRUE);
            request.add(stringrequest);
        }



    }


    public void getStringGETJsonData(final String url, Listener<String> listener, ErrorListener errorlistener,Long time) {

        if(request.getCache().get(url)!=null){
            Long startTime = request.getCache().get(url).serverDate;
            Long nowTime = new Date().getTime();
            if ((nowTime - startTime)> time){                                 //每4小时更新一次数据
                StringRequest stringrequest = new StringRequest(Method.GET, url, listener, errorlistener){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        return super.getHeaders();
                    }
                };
                stringrequest.setShouldCache(Boolean.TRUE);
                request.add(stringrequest);
            }else {
                listener.onResponse(new String(request.getCache().get(url).data));
            }
        }else {
            StringRequest stringrequest = new StringRequest(Method.GET, url, listener, errorlistener){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return super.getHeaders();
                }
            };
            stringrequest.setShouldCache(Boolean.TRUE);

            request.add(stringrequest);
        }
	}


	/**
	 * 获取StringJson数据 POST方式
	 * 
	 * @param url
	 *            url地址
	 * @param listener
	 *            成功接口
	 * @param errorlistener
	 *            失败接口
	 * @param map
	 *            参数
	 */
	public void getStringPOSTJsonData(final String url,final HashMap<String, String> map , Listener<String> listener, ErrorListener errorlistener) {
		StringRequest stringrequest = new StringRequest(Method.POST, url, listener, errorlistener) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				return map;
			}
		};
		stringrequest.setShouldCache(Boolean.TRUE);
		request.add(stringrequest);
	}


    public void getStringPOSTJsonData(final String url, final HashMap<String, String> map,
                                      Listener<String> listener,
                                      ErrorListener errorlistener,
                                      final String token) {
        StringRequest stringrequest = new StringRequest(Method.POST, url, listener, errorlistener) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                if (token != null && !token.equals("")){
                    headers.put("Authorization",token);
                }
                    return headers;
            }
        };
        stringrequest.setShouldCache(Boolean.TRUE);
        request.add(stringrequest);
    }
}
